package com.example.zxw.net

import ApiException
import com.example.zxw.entity.CodeException
import com.example.zxw.ui.views.RequestResult
import com.google.gson.annotations.SerializedName

/**
 *@time 2022/11/16 9:54
 *@author yangqing
 *@describe
 */
data class ApiResult<T>(
    @SerializedName("code") val code: Int,
    val data: T?,
    @SerializedName("msg") val msg: String,
    val timeStamp: Long,
) {
    fun transfrom(): T {
        if (code == 0 && data != null) {
            return data
        } else {
            throw ApiException(code, msg)
        }
    }

    fun dealResult() :RequestResult<T>{
        return if (code == 0 && data != null) {
            RequestResult.Success(data)
        } else {
            if(code == CodeException.NODATA_ERROR){
                RequestResult.NoData
            }else{
                RequestResult.Fail(code,msg?:"")
            }
        }
    }


}
